package com.theme.passport.controller.app;

import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.app.utils.AppContextUtil;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.constant.DownloadTypeEnum;
import com.theme.passport.entity.constant.WidgeSizeEnum;
import com.theme.passport.entity.params.WidgePreviews;
import com.theme.passport.entity.theme.UserBuyEntity;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.service.theme.UserBuyService;
import com.theme.passport.service.theme.UserPayOrderService;
import com.theme.passport.service.theme.WidgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("app/user-buy")
@Api("用户购买主题")
public class AppUserBuyController {
    @Autowired
    private UserBuyService userBuyService;

    @Autowired
    private WidgeService widgeService;

    @Autowired
    private UserPayOrderService userPayOrderService;

    @Login
    @GetMapping("/buyVipRecords")
    @ApiOperation("查询用户购买-交易记录:参数page，limit、[type=[0VIP|1主题|2组件|3壁纸]]")
    public R buyVipRecords(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        PageUtils page = userPayOrderService.queryPage(params);
        return R.ok().put("page", page);
    }

    @Login
    @GetMapping("/buyThemeRecords")
    @ApiOperation("查询用户购买-主题记录[只返回购买预览图和名称](参数:page、limit|type 1主题，2组件 3壁纸，| objectid(对象id，可以是主题壁纸组件)")
    public R buyThemeRecords(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        PageUtils page = userBuyService.queryPage(params);
        return R.ok().put("page", page);
    }

    @Login
    @GetMapping("/buyWidgeRecords")
    @ApiOperation("查询用户购买-组件记录")
    public R buyWidgeRecords(@RequestParam Map<String, Object> params){

        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        params.put("type",DownloadTypeEnum.WIDGE.getValue() +"");
        PageUtils page = userBuyService.queryPage(params);
        List<UserBuyEntity> downloadList = (List<UserBuyEntity>) page.getList();
        if (downloadList == null || downloadList.size() <= 0){
            return R.ok().put("page", page);
        }
        List<Long> ids = new ArrayList<>();
        downloadList.forEach(downloadHistoryEntity -> {
            ids.add(downloadHistoryEntity.getObjectId());
        });
        params.put("ids", ids);
        List<WidgeEntity> list = widgeService.queryListBydistinctNameByIds(params);
        list.forEach(theme ->{
            //同名组件列表
            List<WidgeEntity> widgeSizeList = widgeService.queryWidgeListByName(theme.getName());
            if (widgeSizeList != null && widgeSizeList.size() >0){
                WidgeEntity widge = widgeSizeList.get(0);
                //SysUserEntity byId = sysUserService.getById(widge.getUserId());
                //widge.setUseNickname(byId.getNickname());
                BeanUtils.copyProperties(widge,theme);
                // 同名组件大小
                WidgePreviews widgePreviews = new WidgePreviews();
                widgeSizeList.forEach(item ->{
                    String widgetSize = item.getWidgetSize();
                    if (widgetSize.equals(WidgeSizeEnum.SMALL.getValue())){
                        widgePreviews.setSmall(item.getPreview());
                    }
                    if (widgetSize.equals(WidgeSizeEnum.MEDIUM.getValue())){
                        widgePreviews.setMidium(item.getPreview());
                    }
                    if (widgetSize.equals(WidgeSizeEnum.LAREGE.getValue())){
                        widgePreviews.setLarge(item.getPreview());
                    }
                });
                theme.setWidgePreviews(widgePreviews);
                theme.setWidgeSizeList(widgeSizeList);
            }
        });
        page.setList(list);
        return R.ok().put("page", page);
    }

    @Login
    @GetMapping("/buyWidgeRecords/v2")
    @ApiOperation("查询用户购买-组件记录v2")
    public R buyWidgeRecordsV2(@RequestParam Map<String, Object> params){

        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        params.put("type",DownloadTypeEnum.WIDGE.getValue() +"");
        PageUtils page = userBuyService.queryPage(params);
        List<UserBuyEntity> downloadList = (List<UserBuyEntity>) page.getList();
        if (downloadList == null || downloadList.size() <= 0){
            return R.ok().put("page", page);
        }
        List<Long> ids = new ArrayList<>();
        downloadList.forEach(downloadHistoryEntity -> {
            ids.add(downloadHistoryEntity.getObjectId());
        });
        params.put("ids", ids);
        List<WidgeEntity> list = widgeService.queryListBydistinctNameByIds(params);
        List<WidgeEntity> chilendList = new ArrayList<>();
        for (WidgeEntity theme : list) {
            List<WidgeEntity> widgeSizeList = widgeService.queryWidgeListByNameV2(theme.getName());
            chilendList.addAll(widgeSizeList);
        }
        for (WidgeEntity theme : list) {
            // 同名组件大小
            WidgePreviews widgePreviews = new WidgePreviews();
            for (WidgeEntity citem : chilendList) {
                if (theme.getName().equals(citem.getName())) {
                    String widgetSize = citem.getWidgetSize();
                    if (widgetSize.equals(WidgeSizeEnum.SMALL.getValue())) {
                        widgePreviews.setSmall(citem.getPreview());
                    }
                    if (widgetSize.equals(WidgeSizeEnum.MEDIUM.getValue())) {
                        widgePreviews.setMidium(citem.getPreview());
                    }
                    if (widgetSize.equals(WidgeSizeEnum.LAREGE.getValue())) {
                        widgePreviews.setLarge(citem.getPreview());
                    }
                    BeanUtils.copyProperties(citem, theme);
                }
            }
            theme.setWidgePreviews(widgePreviews);
        }
        List<WidgeEntity> returnList = new ArrayList<>();
        for (WidgeEntity item : list) {
            for (WidgeEntity citem : chilendList) {
                if (item.getName().equals(citem.getName())) {
                    WidgeEntity entity = new WidgeEntity();
                    BeanUtils.copyProperties(item, entity);
                    List<WidgeEntity> tempList = new ArrayList<>();
                    tempList.add(citem);
                    entity.setWidgeSizeList(tempList);
                    entity.setWidgetSize(tempList.get(0).getWidgetSize());
                    returnList.add(entity);
                }
            }
        }
        page.setList(returnList);
        return R.ok().put("page", page);
    }

    @Login
    @GetMapping("/queryIsAlreadyBuy")
    @ApiOperation("查询用户已购过主题组件壁纸[单指某一个](type 1主题，2组件 3壁纸，| objectid(对象id，可以是主题壁纸组件的id)")
    public R queryIsAlreadyBuy(@RequestParam(value = "type") int type,
                               @RequestParam(value = "objectId") Long objectId){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        Map<String, Object> params = new HashMap<>();
        params.put("userId",currentUser.getId() +"");
        params.put("type", String.valueOf(type));
        params.put("objectId",String.valueOf(objectId));
        List<UserBuyEntity> userBuyList = userBuyService.queryList(params);
        if (userBuyList != null && userBuyList.size() > 0){
            return R.ok().put("userBuy",userBuyList.get(0));
        }
        return R.ok().put("userBuy",null);
    }
    @Login
    @PostMapping("/delete")
    @ApiOperation("删除购买记录接口 id数组-购买记录的id")
    public R delete(@RequestBody Long[] ids){
        userBuyService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


}
